package com.example.j280_webproject.day037;

import java.sql.*;

public class JDBC_Login {
    public boolean checkLogin(String username, String password) throws ClassNotFoundException, SQLException {
        String driver = "com.mysql.jdbc.Driver";   // 固定写法
        Class.forName(driver);                     // 通过反射机制 把指定的类加载到当前类中

        // WARNing
        String url = "jdbc:mysql://localhost:3306/mygxa?useSSL=false&characterEncoding=UTF-8";
        String user = "root";
        String pass = "root";
        Connection conn = DriverManager.getConnection(url, user, pass);    // JDBC数据库连接池
        System.out.println("连接成功");

        String sql = "select password from user where username=? limit 1";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1,username);
        ResultSet resultSet = preparedStatement.executeQuery();
        if(resultSet.next()) {
            return resultSet.getString("password").equals(password);
        }
        return false;
    }
}
